package com.xmb.wechat.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.xmb.wechat.R;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/17 15:13
 */
public class WeChatWithDrawBar extends View {

    private int circle_x, circle_y;
    private int circleRadius, smallCircleRadius;

    private Paint mGreenCirclePaint, mGrayCirclePaint, mBitmapPaint;
    private int    mHeight;
    private Bitmap mBitmap;
    private RectF  mRectF;
    private int    mHalfImageWidth;
    private int    mDistance, mSmallDistance;

    public WeChatWithDrawBar(Context context) {
        this(context, null);
    }

    public WeChatWithDrawBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeChatWithDrawBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        circleRadius = SizeUtils.dp2px(6.5f);
        smallCircleRadius = SizeUtils.dp2px(0.9f);
        mHalfImageWidth = SizeUtils.dp2px(38) / 2;
        mDistance = SizeUtils.dp2px(10);
        mSmallDistance = SizeUtils.dp2px(2);

        mGreenCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGreenCirclePaint.setColor(getResources().getColor(R.color.green));
        mGreenCirclePaint.setStyle(Paint.Style.FILL);

        mGrayCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mGrayCirclePaint.setColor(getResources().getColor(R.color.black_35));
        mGrayCirclePaint.setStyle(Paint.Style.FILL);

        mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitmapPaint.setStyle(Paint.Style.FILL);

        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.kinda_wxp_transfer_status_delay_wait);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        circle_x = getWidth() / 2;
        mHeight = getHeight();
        mRectF = new RectF(circle_x - mHalfImageWidth, mHeight / 2 - mHalfImageWidth, circle_x + mHalfImageWidth, mHeight / 2 + mHalfImageWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //顶部圆点
        int topY = circleRadius + getPaddingTop();
        canvas.drawCircle(circle_x, topY, circleRadius, mGreenCirclePaint);
        //绿色小点
        circle_y = topY + mDistance;
        while (circle_y < mRectF.top) {
            canvas.drawCircle(circle_x, circle_y, smallCircleRadius, mGreenCirclePaint);
            circle_y = circle_y + smallCircleRadius * 2 + mSmallDistance;
        }

        //中间图片
        canvas.drawBitmap(mBitmap, null, mRectF, mBitmapPaint);

        //底部圆点
        int bottomY = mHeight - getPaddingBottom() - circleRadius;
        canvas.drawCircle(circle_x, bottomY, circleRadius, mGrayCirclePaint);

        //灰色小点
        circle_y = Float.valueOf(mRectF.bottom).intValue() + smallCircleRadius * 2;
        while (circle_y < bottomY - mDistance) {
            canvas.drawCircle(circle_x, circle_y, smallCircleRadius, mGrayCirclePaint);
            circle_y = circle_y + smallCircleRadius * 2 + mSmallDistance;
        }
    }
}
